package com.swsc.solarverse.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.common.model.PageInfo;
import com.swsc.solarverse.common.util.BizAssert;
import com.swsc.solarverse.dao.entity.DefectDO;
import com.swsc.solarverse.dao.mapper.DefectMapper;
import com.swsc.solarverse.model.*;
import com.swsc.solarverse.service.IDefectService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-06-28
 */
@Service
public class DefectServiceImpl extends ServiceImpl<DefectMapper, DefectDO> implements IDefectService {

    @Override
    public DefectDO getAndCheck(Integer organNo, Integer id) {
        DefectDO defectDo = baseMapper.selectOne(Wrappers.lambdaQuery(DefectDO.class)
                .eq(null != organNo, DefectDO::getOrganNo, organNo)
                .eq(DefectDO::getId, id)
        );
        BizAssert.isFalse(null == defectDo, "故障不存在");
        return defectDo;
    }

    @Override
    public DefectDO getByDefectNo(Long defectNo) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(DefectDO.class).eq(DefectDO::getDefectNo, defectNo));
    }

    @Override
    public List<CorporationDefectLossBO> statisticsCorporationDefectLoss(DefectQueryBO queryBo) {
        return baseMapper.statisticsCorporationDefectLoss(queryBo);
    }

    @Override
    public DefectSumBO defectSum(DefectQueryBO queryBo) {
        return baseMapper.defectSum(queryBo);
    }

    @Override
    public LocalDateTime maxPatrolTime(Integer organNo) {
        return baseMapper.maxPatrolTime(organNo);
    }

    @Override
    public LocalDateTime maxCategoryPatrolTime(Integer organNo, String faultCategory) {
        return baseMapper.maxCategoryPatrolTime(organNo, faultCategory);
    }

    @Override
    public List<DefectRepeatSumBO> statisticsRepeatDefect(DefectQueryBO queryBo) {
        return baseMapper.statisticsRepeatDefect(queryBo);
    }

    @Override
    public Integer avgDealTime(DefectQueryBO queryBo) {
        return baseMapper.avgDealTime(queryBo);
    }

    @Override
    public List<DefectSumBO> chartDefectSumModel(List<Integer> organNoIn) {
        return baseMapper.chartDefectSumModel(organNoIn);
    }

    @Override
    public List<DefectSumBO> chartPatrolSum(List<Integer> organNoIn) {
        return baseMapper.chartPatrolSum(organNoIn);
    }

    @Override
    public List<DefectSumBO> chartDefectSumDay(List<Integer> organNoIn, Date start, Date end) {
        return baseMapper.chartDefectSumDay(organNoIn, start, end);
    }

    @Override
    public List<DefectSumBO> chartDefectSumMonth(List<Integer> organNoIn, Date start, Date end) {
        return baseMapper.chartDefectSumMonth(organNoIn, start, end);
    }

    @Override
    public List<DefectSumBO> chartDefectSumYear(List<Integer> organNoIn, Date start, Date end) {
        return baseMapper.chartDefectSumYear(organNoIn, start, end);
    }

    @Override
    public List<ChartDefectTypeSumBO> chartDefectTypeSum(List<Integer> organNoIn, LocalDateTime startTime, LocalDateTime endTime, Collection<String> faultCategoryIn) {
        return baseMapper.chartDefectTypeSum(organNoIn, startTime, endTime, faultCategoryIn);
    }

    @Override
    public List<DefectWarnStatisticsBO> defectWarn(List<Integer> organNoIn, LocalDateTime startTime, LocalDateTime endTime, Collection<String> faultCategoryIn) {
        return baseMapper.defectWarn(organNoIn, startTime, endTime, faultCategoryIn);
    }

    @Override
    public void saveOrUpdateDefect(DefectDO defectDo) {
        if (null != defectDo.getId()) {
            baseMapper.updateById(defectDo);
        } else {
            DefectDO rawDefectDo = baseMapper.selectOne(Wrappers.lambdaQuery(DefectDO.class).eq(DefectDO::getDefectNo, defectDo.getDefectNo()));
            if (null != rawDefectDo) {
                defectDo.setId(rawDefectDo.getId());
                defectDo.setCreateTime(rawDefectDo.getCreateTime());
                defectDo.setUpdateTime(LocalDateTime.now());
                baseMapper.updateById(defectDo);
            } else {
                baseMapper.insert(defectDo);
            }
        }
    }

    @Override
    public List<PatrolTimeBO> listPatrolTime(Integer organNo, String faultCategory) {
        return baseMapper.listPatrolTime(organNo, faultCategory);
    }

    @Override
    public List<StationZoneBO> listZones(Integer organNo) {
        return baseMapper.listZones(organNo);
    }

    @Override
    public List<UavFlyRecordCountBO> uavPatrolSum(List<Integer> organNoIn) {
        return baseMapper.uavPatrolSum(organNoIn);
    }

    @Override
    public List<String> getDefectTypes(Integer organNo, LocalDateTime start, LocalDateTime end, String faultCategory) {
        return baseMapper.getDefectTypes(organNo, start, end, faultCategory);
    }

    @Override
    public List<ChartDefectTypeSumBO> chartDefectTypeDeviceSum(DefectQueryBO queryBo) {
        return baseMapper.chartDefectTypeDeviceSum(queryBo);
    }

    @Override
    public List<DefectDO> listStationDefectBO(DefectQueryBO queryBo) {
        return baseMapper.listStationDefectBO(queryBo);

    }

    @Override
    public PageInfo<DefectDO> page(DefectQueryBO queryBo, Integer pageNo, Integer pageSize) {
        Page<DefectDO> page = baseMapper.listStationDefectBO(queryBo, new Page<>(pageNo, pageSize));
        return new PageInfo<>(page.getRecords(), page.getTotal(), pageNo, pageSize);
    }

    @Override
    public DefectStatisticsBO defectStatistics(Integer organNo) {
        return baseMapper.defectStatisticsByOrganNo(organNo);
    }

    @Override
    public DefectStatisticsBO defectStatistics(Integer organNo, String startDate, String endDate) {
        return baseMapper.defectStatistics(organNo, startDate, endDate);
    }

    @Override
    public PageInfo<DefectStatisticsBO> pageStatisticsDefectCount(Integer organNo, Integer repeatTimes, Integer pageNo, Integer pageSize) {
        Page<DefectStatisticsBO> page = baseMapper.pageStatisticsDefectCount(organNo, repeatTimes, new Page<>(pageNo, pageSize));
        return new PageInfo<>(page.getRecords(), page.getTotal(), pageNo, pageSize);
    }

    @Override
    public List<Map<String, Object>> statisticsDefectModel(Integer organNo) {
        return baseMapper.statisticsDefectModel(organNo);
    }
}
